package com.coderman.tianhua.idgenerator.common.bean;

import lombok.Data;

/**
 * @author haozi
 * @date 2020/5/811:49 上午
 */
@Data
public class ResponseResult<T> {
    private final static transient int OK_CODE = 0;
    private final static transient int ERROR_CODE = 1;

    private Integer code;
    private String msg;
    private T data;
    private long timestamp;

    public static <T> ResponseResult success(T data) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(OK_CODE);
        responseResult.setTimestamp(System.currentTimeMillis());
        responseResult.setData(data);
        return responseResult;
    }

    public static ResponseResult error() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ERROR_CODE);
        responseResult.setTimestamp(System.currentTimeMillis());
        return responseResult;
    }

    public static ResponseResult error(String msg) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ERROR_CODE);
        responseResult.setMsg(msg);
        responseResult.setTimestamp(System.currentTimeMillis());
        return responseResult;
    }

    public boolean isSuccess() {
        return code != null && code == OK_CODE;
    }
}
